<?php
/**
 * @name IndexController
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */

//namespace  UserNamespace;

error_reporting(E_ALL^E_NOTICE^E_WARNING);
class UserController extends Yaf_Controller_Abstract {
    private $arrConfig='';
    private $salt='';
    private $salt_time='';
	
	
	public function init(){
		
		$arrConfig = Yaf_Application::app()->getConfig();
		$this->arrConfig=$arrConfig;
		$this->salt=$arrConfig->database->params->salt;
		$this->salt_time=$arrConfig->database->params->salt_time;
		//$salt=$arrConfig->database->params->salt;
	}
	
	public function checklogin1(){
		if(md5($this->salt.$_SESSION['user_token_time'].$_SESSION['user_id'])!=$_SESSION['user_token']){
			exit(json_encode(array('status'=>0,'info'=>'请先登陆后操作')));
		}
	}
	
	public function checklogin2(){
		$info=json_decode(file_get_contents("php://input"),true);
		$salt=$info['salt'] ? $info['salt'] : $_REQUEST['salt'];
	
	
		$where['salt= ?']=$salt;
		$usermodel=new UserModel();
		$user=$usermodel->getuser($where);
		if(!$user || time()-$user['salt_time']>$this->salt_time){
			exit(json_encode(array('status'=>0,'info'=>'登陆状态超时')));
		}
	}
	
	
	public function messcodeAction(){
		$model = new messcodeModel();
		
		$info=json_decode(file_get_contents("php://input"),true);
        $mobile=$info['mobile'] ? $info['mobile'] : $_REQUEST['mobile'];
		
        //  echo  $this->getRequest()->getPost("name"); //old_name
        //验证码
//       $verify=$info['verify'] ? $info['verify'] : $_REQUEST['verify'];
//        if(!$verify){
//          exit(json_encode(array('status'=>0,'info'=>'验证码错误')));
//        }
      // $mobile=$this->getRequest()->getPost('mobile',false);
		
		
		//$mobile= ($_REQUEST['mobile'])?htmlspecialchars($_REQUEST['mobile']):"";
		
		if(preg_match("/^1[0-9]{2}[0-9]{8}$|15[01389]{1}[0-9]{8}$|189[0-9]{8}$/",$mobile)){
			//验证通过
			$model = new MesscodeModel();
			//print_r($array); die();
			$list = $model->sendcode($mobile);
			//print_r($list);
			echo json_encode($list);
		}else{
			//手机号码格式不对
		     exit(json_encode(array('status'=>0,'info'=>'手机格式不对')));
		}
	//	preg_match_all("/13[123569]{1}\d{8}|15[1235689]\d{8}|188\d{8}/",$mobile,$array);
	}
	
	private function password_generate($password){
		$salt=$this->salt;
		return md($salt.$password);
	}
	
	
	
	
	public function logincheckAction(){
		//exit('sdsdfsdfs');
		$info=json_decode(file_get_contents("php://input"),true);
		$salt=$info['salt'] ? $info['salt'] : $_REQUEST['salt'];
		
		
		$where['salt= ?']=$salt;
		$usermodel=new UserModel();
		$user=$usermodel->getuser($where);
		//if(1558511490>1526975744){echo 123;} die();
	//	echo $user['salt_time']+$this->salt_time; echo ('_'); echo time();die();
// 	echo time();
// 	echo '<br/>';
// 	echo intval($user['salt_time']+$this->salt_time);
	$stime=$user['salt_time']+$this->salt_time;
	if(!$user || $stime<time()){
		exit(json_encode(array('status'=>0,'info'=>'登陆状态超时')));
		
	}else{
		exit(json_encode(array('status'=>1,'info'=>'登陆状态正常','data'=>$user)));
	}
// 		if(!$user || $stime < time()  ){
// 			exit(json_encode(array('status'=>1,'info'=>'登陆状态正常','data'=>$user)));
// 		}else{
// 			exit(json_encode(array('status'=>0,'info'=>'登陆状态超时')));
// 		}
	}
	
	public function logoutAction(){
		
		$info=json_decode(file_get_contents("php://input"),true);
		$mobile=$info['mobile'] ? $info['mobile'] : $_REQUEST['mobile'];
		//$mobile= ($_REQUEST['mobile'])?htmlspecialchars($_REQUEST['mobile']):"";
		
		$where['mobile= ?']=$mobile;
		$usermodel=new UserModel();
		$user=$usermodel->getuser($where);
		
		if($user){
			//$data['salt']=md5($mobile.rand(1000,9999));
			$REQUEST_TIME=$_SERVER['REQUEST_TIME'];
			$data['salt']=md5($this->salt.$REQUEST_TIME.$user['id']);
			$data['salt_time']=$REQUEST_TIME;
			
			$where['mobile= ?']=$mobile;
			$res=$usermodel->updateuser($data,$where);
			unset($_SESSION['user_token']);
			if($res){
				exit(json_encode(array('status'=>1,'info'=>'退出成功')));
			}else{
				exit(json_encode(array('status'=>0,'info'=>'退出失败')));
			}
		}else{
			exit(json_encode(array('status'=>0,'info'=>'号码不存在')));
		}
	}

	
	public function loginAction(){
		
		//getPost/getQuery/getServer/getCookie
// 		$request = $this->getRequest()->getQuery('mobile',0);
// 		$mobile=$this->getRequest()->getPost('mobile',false);
// 		$smscode=$this->getRequest()->getPost('smscode',false);
// 		echo $mobile;
// 		//print_r($request); 
// 		die();
		$info=json_decode(file_get_contents("php://input"),true);
		$mobile=$info['mobile'] ? $info['mobile'] : $_REQUEST['mobile'];
		$smscode=$info['smscode'] ? $info['smscode'] : $_REQUEST['smscode'];
		//渠道
// 		$submit=$info['submit'] ? $info['submit'] : $_REQUEST['submit'];
// 		if($submit!='ele'){
// 			exit(json_encode(array('status'=>0,'info'=>'非正常渠道')));
// 		}
		//$smscode=$_REQUEST['smscode'];
		
		//$mobile= ($_REQUEST['mobile'])?htmlspecialchars($_REQUEST['mobile']):"";
		
		if(preg_match("/^1[0-9]{2}[0-9]{8}$|15[01389]{1}[0-9]{8}$|189[0-9]{8}$/",$mobile)){
			
			$model = new MesscodeModel();
			//验证通过     //错误三次拒绝
			$list=$model->getcode($mobile,$smscode);
			if($list['ctime'] > time()){
				$usermodel=new UserModel();
				$where['mobile= ?']=$mobile;
				$user=$usermodel->getuser($where);
				
				session_start();
				if(!$user){
					$userid=$usermodel->adduser($mobile);
					$where['id= ?']=$userid;
					$user=$usermodel->getuser($where);
					
					$REQUEST_TIME=$_SERVER['REQUEST_TIME'];
					$_SESSION['user_token']=md5($this->salt.$REQUEST_TIME.$user['id']);
					$_SESSION['user_token_time']=$REQUEST_TIME;
					$_SESSION['user_id']=$user['id'];
					
					
					exit(json_encode($user));
				}else{
					
                	$REQUEST_TIME=$_SERVER['REQUEST_TIME'];
					$_SESSION['user_token']=md5($this->salt.$REQUEST_TIME.$user['id']);
					$_SESSION['user_token_time']=$REQUEST_TIME;
					$_SESSION['user_id']=$user['id'];

					
					$data['salt']=md5($this->salt.$REQUEST_TIME.$user['id']);
					$data['salt_time']=$REQUEST_TIME;
					$where['mobile= ?']=$mobile;
					$res=$usermodel->updateuser($data,$where);
					$user=$usermodel->getuser($where);
					exit(json_encode($user));
				}
				
			}else{
			  exit(json_encode(array('status'=>0,'info'=>'验证码超时或者用户不存在')));
			}
		}else{
			//手机号码格式不对
			exit(json_encode(array('status'=>0,'info'=>'手机格式不对')));
			//exit(Common_Request::reponse(0,'手机格式不对'));
		}
	}
	
	
	
	public function categoryListAction(){
		$info=json_decode(file_get_contents("php://input"),true);
		$catid=$info['catid'] ? $info['catid'] : $_REQUEST['catid'];
		//$catid=$_REQUEST['catid'] ? $_REQUEST['catid'] : '';
		$model = new CategoryModel();
		
		
		if($catid){
			$where['catid = ?']=$catid;
		}else{
			$where='';
		}
		
		$list = $model->getList($where);
		//print_r($list); 
		echo json_encode($list);
	}
	
}
